using System;
using System.Collections.Generic;
using System.Linq;

namespace DIS_DAL_WS.Repositories
{
	#region using
	using DIS_DAL_WS.Models;
	using DIS_DAL_WS.Helpers;
	#endregion
	public class OrderDetailRepository
	{
		public List<OrderDetail> SelectAll()
		{
			try
			{
				return DataContext.Instance.OrderDetail.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<OrderDetail>();
		}

		public OrderDetail SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.OrderDetail.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(OrderDetail obj)
		{
			try
			{
				DataContext.Instance.OrderDetail.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.OrderDetail.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.OrderDetail.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

        public bool DeleteByOrder(int idoder)
        {
            try
            {
                var lstItem = SelectByIDOrder(idoder);
                foreach (var orderdetail in lstItem)
                {
                    var delitem = DataContext.Instance.OrderDetail.FirstOrDefault(item => item.ID == orderdetail.ID);
                    DataContext.Instance.OrderDetail.DeleteObject(delitem);
                    DataContext.Instance.SaveChanges();
                } 
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return false;
        }

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<OrderDetail> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.OrderDetail where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<OrderDetail>();
		}

		public List<OrderDetail> SelectByIDOrder(int idorder)
		{
			try
			{
				var lstItem = (from item in DataContext.Instance.OrderDetail where item.IDOrder == idorder select item).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<OrderDetail>();
		}

		public List<OrderDetail> SelectByIDItem(int iditem)
		{
			try
			{
				var lstItem = (from item in DataContext.Instance.OrderDetail where item.IDItem == iditem select item).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<OrderDetail>();
		}

	}
}